package com.sxkj.gis.wms.comm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Service;

import com.sxkj.gis.wms.comm.service.WmsCommService;

@Service("wmsCommService")
public class WmsCommServiceImpl implements WmsCommService {
	@Override
	public void deleteFile(String picName, String basePath, String saveDir) {
		String picPath = basePath + saveDir + "\\" + picName;
		File file = new File(picPath);
		if (file.exists()) {
			// 把修改之前的图片删除 以免太多没用的图片占据空间
			file.delete();
		}
	}

	@Override
	public void saveFile(String picName, byte[] buffer, String basePath, String saveDir) {
		String saveFilePath = basePath + saveDir;
		/* 构建文件目录 */
		File fileDir = new File(saveFilePath);
		if (!fileDir.exists()) {// 如果目录不存在，那么就创建目录
			fileDir.mkdirs();
		}
		try {
			FileOutputStream out = new FileOutputStream(saveFilePath + "\\" + picName);
			out.write(buffer);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public byte[] getFile(String picName, String basePath, String saveDir) throws IOException {
		String picPath = basePath + saveDir + "\\" + picName;
		File file = new File(picPath);

		FileInputStream inputStream = new FileInputStream(file);
		int i = inputStream.available();
		byte[] buff = new byte[i];
		inputStream.read(buff);
		inputStream.close();
		return buff;
	}
}
